package com.ssm.web;

import com.ssm.dao.UserDao;
import com.ssm.entity.Log;
import com.ssm.entity.User;
import com.ssm.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by bai on 2017/8/1.
 */
@Controller
@RequestMapping("/log")
public class LogController {

    @Autowired(required = false)
    private LogService logService;

    @Autowired(required = false)
    UserDao userDao;

    @RequestMapping(value = "/logList", method = RequestMethod.GET)
    private String logViewPage(User user, Model model,HttpServletRequest request ) {

        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        /**
         * 判断当前用户是否为管理员
         * 是则查询所有日志
         * 否则查询用户名下的日志
         * */
        User testUser = userDao.queryById(1);
        System.out.print(testUser.getUsername());

        if(testUser.getUsername().equals("admin")) {

            //查询数据库中所有的日志
            List<Log> logList = logService.queryAll();
            System.out.print(logList);
            model.addAttribute("logList", logList);
        }
        else
        {
            List<Log> logList = logService.queryByUserName(testUser.getUsername());
            model.addAttribute("logList", logList);
        }

        return "logList";

    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.GET)
    private String deleteallLog(User user, Model model,HttpServletRequest request ) {

        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        /**
         * 判断当前用户是否为管理员
         * 是则删除所有日志
         * 否则删除用户名下的日志
         * */
        User testUser = userDao.queryById(1);
        System.out.print(testUser.getUsername());

        if(testUser.getUsername().equals("admin")) {

            //删除数据库中所有的日志
            logService.deleteAll();
            /*System.out.print(logList);*/
            /*model.addAttribute("logList", logList);*/
        }
        else
        {
            logService.deleteLogByUserId(testUser.getId());
            /*List<Log> logList = logService.queryByUserName(testUser.getUsername());
            model.addAttribute("logList", logList);*/
        }

        return "logList";

    }


}
